import cv2 as cv
import sys
import os
from python_ai.common.xcommon import *
import matplotlib.pyplot as plt
import numpy as np
import time

cam_idx = 0
try:
    cap = cv.VideoCapture(cam_idx)
except cv.error as ex:
    print(f'Cannot open camera {cam_idx}')
    print(ex)
    sys.exit(1)

if not cap.isOpened():
    print(f'Cannot open camera {cam_idx}')



FPS = 25
interval = 1000 // FPS
first = True
while True:
    ret, frame = cap.read()

    if not ret:
        print('Cannot receive frame. Exiting ...')
        sys.exit(0)

    if first:
        print(frame.shape)  # (480, 640, 3)
        first = False

    # frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)  # no need, my cam is alread RGB

    cv.imshow('Cam', frame)

    k = cv.waitKey(interval) & 0xFF
    if k == ord('q') or k == ord('Q'):
        break

cap.release()
cv.destroyAllWindows()
